package com.song.web;

import com.song.service.EmpService;
import com.song.service.impl.EmpServiceImpl;
import com.song.util.ThUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.web.IWebExchange;
import org.thymeleaf.web.servlet.JakartaServletWebApplication;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author 宋伟宁
 * @Date 2023/9/7
 * @Version 1.0
 **/
@WebServlet(urlPatterns = {"/list"})
public class EmpServlet extends HttpServlet {

    //创建业务对象
    private static final EmpService SERVICE= new EmpServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String servletPath = req.getServletPath();
        System.out.println(servletPath);
        //创建session作用域对象
        IWebExchange webExchange = JakartaServletWebApplication.buildApplication(getServletContext())
                .buildExchange(req, resp);
        //创建用于上下文传值的对象
        WebContext webContext = new WebContext(webExchange);
        //创建ajax使用的输出流
        PrintWriter out = resp.getWriter();

        switch (servletPath){
             case "/list" ->{
                  //封装数据到作用域
                  webContext.setVariable("list",SERVICE.findAll());
                  //传递数据集合到页面
                 ThUtils.print("/view/list.html",webContext,resp);
             }
        }


    }
}
